package com.zh.work.controller.platform.channel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.channel.QrCodeMerchMapper;
import com.zh.work.entity.channel.QrCodeMerchEntity;
import com.zh.work.service.platform.channel.QrCodeMerchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码商户管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/04
 * @Version: 1.0.1
 */
@Api(value = "二维码商户管理控制器", produces = "application/json", tags = {"二维码商户管理控制器"})
@RestController
@RequestMapping("/qrCodeMerch")
public class QrCodeMerchController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/04
     */
    @Autowired
    private QrCodeMerchMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/04
     */
    @Autowired
    private QrCodeMerchService service;


    @ApiOperation(value = "新增一条记录", tags = {"putAddQrCodeMerch"}, notes = "新增一条记录")
    @PutMapping("/putAddQrCodeMerch")
    @ResponseBody
    public JSONResult<Object> putAddQrCodeMerch(@ApiParam(value = "二维码商户实体类", name = "qrcodeMerchEntity") @RequestBody QrCodeMerchEntity qrcodeMerchEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (qrcodeMerchEntity.insert()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModQrCodeMerch"}, notes = "通过id修改信息")
    @PutMapping("/putModQrCodeMerch")
    @ResponseBody
    public JSONResult<Object> putModQrCodeMerch(@ApiParam(value = "二维码商户实体类", name = "qrcodeMerchEntity") @RequestBody QrCodeMerchEntity qrcodeMerchEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (qrcodeMerchEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteQrCodeMerchById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteQrCodeMerchById")
    public JSONResult<Object> deleteQrCodeMerchById(@ApiParam(value = "二维码商户实体类", name = "qrcodeMerchEntity") QrCodeMerchEntity qrcodeMerchEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (qrcodeMerchEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteQrCodeMerchBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteQrCodeMerchBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteQrCodeMerchBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getQrCodeMerchById"}, notes = "根据id查询详细信息")
    @GetMapping("/getQrCodeMerchById")
    public JSONResult<QrCodeMerchEntity> getQrCodeMerchById(@ApiParam(value = "二维码商户实体类", name = "qrcodeMerchEntity") QrCodeMerchEntity qrcodeMerchEntity) {
        JSONResult<QrCodeMerchEntity> jsonResult = new JSONResult<>();
        try {
            QrCodeMerchEntity result = qrcodeMerchEntity.selectById(qrcodeMerchEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesQrCodeMerchs"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesQrCodeMerchs")
    public JSONResult<List<QrCodeMerchEntity>> getPagesQrCodeMerchs(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "二维码商户实体类", name = "qrCodeMerchEntity") QrCodeMerchEntity qrCodeMerchEntity) {
        JSONResult<List<QrCodeMerchEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QrCodeMerchEntity> result = this.service.getPagesQrCodeMerchs(new Page<>(pageNo, pageSize), qrCodeMerchEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改二维码商户状态", tags = {"putModQrCodeMerchantStatus"}, notes = "修改二维码商户状态")
    @PutMapping("/putModQrCodeMerchantStatus")
    @ResponseBody
    public JSONResult<Object> putModQrCodeMerchantStatus(@ApiParam(value = "二维码商户实体类", name = "qrCodeMerchEntity")
                                                          @RequestBody QrCodeMerchEntity qrCodeMerchEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = this.service.modQrCodeMerchantStatus(qrCodeMerchEntity);
            if (i > 0) {
                jsonResult.setMessage("操作成功");
                jsonResult.setSuccess(true);
                jsonResult.setCode(200);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("网络繁忙，请稍后重试!");
            }
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }
}
